package com.initial.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.initial.entity.TbCrmEmailRecord;
import com.initial.entity.TbCrmSmsRecord;
import com.initial.entity.TbSystemUser;
import com.initial.sendemail.EmailServiceImpl;
import com.initial.service.TbCrmEmailRecordService;
import com.initial.service.TbCrmSmsRecordService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class MarketingActionController {
    @Resource
    private TbCrmEmailRecordService tbCrmEmailRecordService;
    @Resource
    private TbCrmSmsRecordService tbCrmSmsRecordService;
    @Resource
    private EmailServiceImpl emailService;
    /**
     *去  邮箱  短息   信息页面
     **/
    @RequestMapping("manage/marketing/toemail_marketing")
    public String tolistmessage(){
        return "views/manage/marketing/email_marketing";
    }
    /**
     * 去  ***   查看发送邮箱的记录 *** 分页查询
     * */
    @RequestMapping("manage/marketing/MarketingAction_sendemail.do")
    public String MarketingActionSendemail(){
        return "views/system/message/list_message";
    }
    /**
     * 分页查询 *****    发邮箱的记录
     * */
    @RequestMapping("system/message/MessageAction_findByPage.action")
    @ResponseBody
    public Map MessageActionFindByPageAction(Integer page,Integer rows){
        Map map=new HashMap();
        IPage<TbCrmEmailRecord> tbCrmEmailRecordIPage = tbCrmEmailRecordService.selectAllPage(new Page<TbCrmEmailRecord>(page, rows),null);
        long total = tbCrmEmailRecordIPage.getTotal();
        List<TbCrmEmailRecord> records = tbCrmEmailRecordIPage.getRecords();
        map.put("totalRows",total);
        map.put("result",records);
        return map;
    }
    /**
     * 去 **** 写邮箱页面
     * */
    @RequestMapping("system/message/MessageAction_write.action")
    public String toMessageAction_write(){
        return "views/manage/marketing/sendemail_marketing";
    }
    /**
     * 接收  发送邮箱内容
     * */
    @RequestMapping("manage/marketing/MarketingAction_sendEmail.do")
    @ResponseBody
    public Map MarketingActionSendEmail(TbCrmEmailRecord tbCrmEmailRecord, HttpSession session){
        Map map = emailService.sendHtmlMail(tbCrmEmailRecord.getToAddr(), tbCrmEmailRecord.getSubject(), tbCrmEmailRecord.getContent());
        boolean success = (boolean) map.get("success");
        if (success){
            tbCrmEmailRecord.setStatus(1);
        }else {
            tbCrmEmailRecord.setStatus(0);
        }
        if (tbCrmEmailRecord.getToAddr()!=null && tbCrmEmailRecord.getSubject()!=null && tbCrmEmailRecord.getContent()!=null){
//            从session获取用户id !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
            TbSystemUser user = (TbSystemUser) session.getAttribute("user");
            tbCrmEmailRecord.setUserId(user.getId());
            boolean save = tbCrmEmailRecordService.save(tbCrmEmailRecord);
        }
        return map;
    }
    /**
     * 去 发信息页面
     * */
    @RequestMapping("/manage/marketing/MarketingAction_sendmsg.do")
    public String MarketingActionSendmsg(){
        return "views/system/message/list_message_noto";
    }

    /**
     * 去  ***   查看发送 短息 的记录 *** 分页查询
     * */
    @RequestMapping("/manage/marketing/MarketingAction_sendmsg_noto")
    @ResponseBody
    public Map MessageActionNoteByPage(Integer page,Integer rows){
        Map map=new HashMap();
        IPage<TbCrmSmsRecord> tbCrmSmsRecordIPage = tbCrmSmsRecordService.selectAllPage(new Page<TbCrmSmsRecord>(page, rows));
        long total = tbCrmSmsRecordIPage.getTotal();
        List<TbCrmSmsRecord> records = tbCrmSmsRecordIPage.getRecords();
        map.put("totalRows",total);
        map.put("result",records);
        System.out.println(map);
        return map;
    }
    /**
     * 去  写发信息内容
     * */
    @RequestMapping("/system/message/MessageAction_write.ssg")
    public String MessageActionWrite(){
        return "views/manage/marketing/sendmsg_marketing";
    }
    /**
     * 接收  发送短信内容
     * */
    @RequestMapping("manage/marketing/MarketingAction_sendMsg.do")
    @ResponseBody
    public Map MarketingActionSsm(HttpSession session,TbCrmSmsRecord tbCrmSmsRecord){
        Map map=new HashMap();
        TbSystemUser user = (TbSystemUser) session.getAttribute("user");
        tbCrmSmsRecord.setUserId(user.getId());
        System.out.println(tbCrmSmsRecord);
        boolean save = tbCrmSmsRecordService.save(tbCrmSmsRecord);
        map.put("success",save);
        return map;
    }
}
